local blaze = require "blaze"
local game = require "club.threecard.roulette"
local consts = game.util.consts

local M = game.ui.activity()

M.store_watchers = {
    gameplay = {
        ["$"] = blaze.noop,

        ["展示结果"] = function(self, store, state, args)
            self:start_coroutine(function()
                self.animator:Play("game_result_clip")
                self.o:play(state.result_view.n)
                game.coroutine.wait(5)
                self.result_shower:result(state.result_view.n)
                game.coroutine.wait(1)
                self.bet_area:result(state.result_view.hits)
            end)
        end,

        ["阶段变更"] = function(self, store, state, args)
            if state.phase_id == consts.PHASE_SETTLE then
                self.animator:Play("game_bet_clip")
            end

            if state.phase_id == consts.PHASE_IDLE or state.phase_id == consts.PHASE_BET then
                self.result_shower:hide()
            end
        end
    }
}

function M:ctor()
    self.res = "club.threecard.roulette/ui/ingame.b:ingame"

    local BetOperator = require "club.threecard.baccaratlib.ui.widgets.BetOperator"
    self.bet_operator = BetOperator.new()

    local PhaseShower = require "club.threecard.baccaratlib.ui.widgets.PhaseShower"
    self.phase_shower = PhaseShower.new()

    local Resumer = require "club.threecard.baccaratlib.ui.widgets.Resumer"
    self.resumer = Resumer.new(self)

    local o = game.require "ui.ingame.o"
    self.o = o.new()
    self.o:hide()

    local player_info = game.gamelib.require "widget.player_info"
    self.player_info = player_info.new()

    local Seats = require "club.threecard.baccaratlib.ui.widgets.Seats"
    self.seats = Seats.new(4, "club.threecard.roulette/ui/ingame.b:seats")

    local Others = require "club.threecard.baccaratlib.ui.widgets.Others"
    self.others = Others.new(self.seats)

    local bet_area_gen = game.require "ui.ingame.bet_area_gen"
    self.bet_area_gen = bet_area_gen.new()

    local BetAreas = require "club.threecard.baccaratlib.ui.gameplay.BetAreas"
    self.bet_area = BetAreas.new("club.threecard.roulette/ui/ingame.b:bet_area")

    local BetChip = require "club.threecard.baccaratlib.ui.gameplay.BetChip"
    self.bet_chip = BetChip.new(self.bet_area, self.seats, self.others, "club.threecard.roulette/ui/ingame.b:bet_chip")

    local result_shower = game.require "ui.ingame.result_shower"
    self.result_shower = result_shower.new()

    local rule = game.require "ui.ingame.rule"
    self.rule = rule.new()
end

function M:load()
    self:super("load")

    self.o:set_gameobject(self.o_root1, true)

    self.player_info:load()
    self.player_info:set_parent(self.self_root, false)

    self.seats:load()
    self.seats:set_parent(self.seats_root, false)

    self.bet_operator:load()
    self.bet_operator:set_parent(self.bet_operator_root, false)

    self.others:load()
    self.others:set_parent(self.others_root, false)

    self.phase_shower:load()
    self.phase_shower:set_parent(self.phase_shower_root, false)

    self.bet_area_gen:load()
    self.bet_area_gen:set_parent(self.bet_area_gen_root, false)
    --self.bet_area_gen:gen()

    self.bet_area:load()
    self.bet_area:set_parent(self.bet_area_root, false)

    self.bet_chip:load()
    self.bet_chip:set_parent(self.bet_chip_root, false)

    self.result_shower:load()
    self.result_shower:set_parent(self.result_root, false)

    self.rule:load()
    self.rule:set_parent(self.rule_root, false)

    self.resumer:load()
    self.resumer:set_parent(self.resumer_root, false)
end

function M:on_injected(bind)
    bind("button.click", self.btn_exit, self.exit_ivk)
    bind("shortcut.escape", self.btn_exit.gameObject, self.exit_ivk)
    bind("button.click", self.btn_rule, self.rule_ivk)

    game.utility.button_style.highlight_scale(self.btn_exit)
    game.utility.button_style.highlight_scale(self.btn_rule)
end

function M:exit_ivk()
    game.game_scene.leave()
end

function M:rule_ivk()
    self.rule:show()
end

function M:on_release()
    self.phase_shower:release()
end

return M